package top.fangw.tenant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.fangw.common.core.entity.BaseIdNameVo;
import top.fangw.tenant.pojo.dto.DatasourceInfoPageQueryDto;
import top.fangw.tenant.pojo.entity.DatasourceInfo;
import top.fangw.tenant.service.IDatasourceInfoService;
import top.fangw.common.core.result.Result;
import top.fangw.common.log.annotation.Log;

import java.util.List;

/**
 * <p>
 * 用途：用于保存系统中除基础信息外的所有数据源信息 前端控制器
 * </p>
 *
 * @author fangw
 * @since 2024-01-16
 */
@Tag(name = "数据源信息管理")
@RestController
@RequestMapping("/datasource-info")
@RequiredArgsConstructor
public class DatasourceInfoController {

    private final IDatasourceInfoService datasourceInfoService;

    @Log
    @Operation(summary ="添加数据源信息")
    @PostMapping("/add")
    public Result<?> add(@RequestBody DatasourceInfo datasourceInfo) {
        int add = datasourceInfoService.add(datasourceInfo);
        return Result.judge(add > 0);
    }

    @Log
    @Operation(summary ="删除数据源")
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam String id) {
        datasourceInfoService.delete(id);
        return Result.success();
    }

    @Log
    @Operation(summary ="分页查询数据源信息")
    @PostMapping("/page")
    public Result<?> page(@RequestBody DatasourceInfoPageQueryDto pageQueryDto) {
        IPage<DatasourceInfo> res = datasourceInfoService.pageQuery(pageQueryDto);
        return Result.success(res);
    }

    @Log
    @Operation(summary ="测试数据源连接")
    @PostMapping("/testConnection")
    public Result<?> testConnection(@RequestBody DatasourceInfo datasourceInfo) {
        datasourceInfoService.testConnection(datasourceInfo);
        return Result.success();
    }
    @Log
    @Operation(summary ="获取数据源下拉信息")
    @GetMapping("/listDataSource")
    public Result<?> listDataSource() {
        List<BaseIdNameVo> list = datasourceInfoService.listDataSource();
        return Result.success(list);
    }
}
